/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package business;

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Thomas
 */
public class MailMgr {

    public static void send(Customer customer, BasketMgr basket) throws MessagingException {

        final String username = "buildmoregreen2012@gmail.com";
        final String password = "fb406472";

        Properties properties = new Properties();
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        properties.setProperty("mail.smtp.host", "smtp.gmail.com");
	properties.setProperty("mail.smtp.host", "smtp.gmail.com");
        properties.setProperty("mail.smtp.port", "587");

        Session session = Session.getInstance(properties,
                new javax.mail.Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
        });

        try {

            String from = customer.getEmail();
            String to = "buildmoregreen2012@gmail.com";
            String subject = "Cmd " + customer.getFirstName() + " " + customer.getLastName().toUpperCase();
            String content = buildContent(customer, basket);

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            message.addRecipient(Message.RecipientType.CC, new InternetAddress(customer.getEmail()));
            message.setSubject(subject);
            message.setContent(content, "text/html");

            Transport.send(message);

        } catch (MessagingException e) {
            throw e;
        }

    }

    private static String buildContent(Customer customer, BasketMgr basket) {

        String content;

        // Client + Adresse
        content  = "<h1>Client</h1>\n";
        content += "<hr/>\n";
        content += "<p>\n";
        content += customer.getFirstName() + "&nbsp;" + customer.getLastName().toUpperCase() + "<br/>\n";
        content += "<a href=\"mailto:" + customer.getEmail() + "\">" + customer.getEmail() + "</a><br/>\n";
        content += "T&eacute;l&eacute;phone : " + customer.getNumTel() + "\n";
        content += "</p>\n";
        content += "<p>\n";
        content += customer.getAddress().getLine1() + "<br/>\n";
        if (customer.getAddress().getLine2().length() > 0) {
            content += customer.getAddress().getLine2()+ "<br/>\n";
        }
        content += customer.getAddress().getZipCode() + "&nbsp;" + customer.getAddress().getCity() + "&nbsp;(" + customer.getAddress().getCountry() + ")\n";
        content += "</p>\n";

        // Produits
        content += "<h1>Produits</h1>\n";
        content += "<hr/>\n";
        content += "<br/>\n";
        content += "<table border=\"1\" width=\"600\">\n";
        content += "<thead>\n";
        content += "<tr>\n";
        content += "<th>R&eacute;f&eacute;rence</th>\n";
        content += "<th>Produit</th>\n";
        content += "<th>Prix</th>\n";
        content += "</tr>\n";
        content += "</thead>\n";
        content += "<tbody>\n";
        for (Product product : basket.getProducts()) {
            content += "<tr>\n";
            content += "<td style=\"text-align: right;\">" + product.getID() + "</td>\n";
            content += "<td>" + product.getName() + "</td>\n";
            content += "<td style=\"text-align: right;\">" + product.getPrice() + "&nbsp;&euro;</td>\n";
            content += "</tr>\n";
        }
        content += "<tfoot>\n";
        content +="<tr>\n";
        content += "<td style=\"text-align: center;\"><strong>Total</strong></td>\n";
        content += "<td style=\"text-align: center;\"><strong>" + basket.getProducts().size() + "&nbsp;article(s)</strong></td>\n";
        content += "<td style=\"text-align: right;\"><strong>" + basket.getTotal() + "&nbsp;&euro;</strong></td>\n";
        content += "</tr>\n";
        content += "</tfoot>\n";

        return content;
    }
}
